package com.dqhl.business.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.business.Base.BaseActivity;
import com.dqhl.business.R;
import com.dqhl.business.adapter.TypeManagerAdapter;
import com.dqhl.business.modle.GoodsType;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;
import com.mobeta.android.dslv.DragSortListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * description: 分类管理
 * autour: guanluocang
 * date: 2016/11/6 14:18
 */
public class GoodsTypeClassifyActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private TextView tv_goodsTopTitle;
    private ImageView iv_goodsGoBack;
    private TextView tv_topRight;
    //    private ListView lv_goodsType;
    private DragSortListView dslvList;
    private TextView tv_no_data;
    private List<GoodsType> goodsTypeList;
    private TypeManagerAdapter typeManagerAdapter;
    private DragSortListView.DropListener onDrop;
    private DragSortListView.RemoveListener onRemove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_type_classify);
        context = this;
        initView();
        initTypeSelect();
    }

    private void initView() {
        (tv_goodsTopTitle) = (TextView) findViewById(R.id.tv_goodsTopTitle);
        (tv_topRight) = (TextView) findViewById(R.id.tv_topRight);
        (iv_goodsGoBack) = (ImageView) findViewById(R.id.iv_goodsGoBack);
        tv_goodsTopTitle.setText("管理分类");
        tv_topRight.setText("保存");
        tv_topRight.setVisibility(View.VISIBLE);
//        lv_goodsType = (ListView) findViewById(R.id.lv_goodsType);
        dslvList = (DragSortListView) findViewById(R.id.dslvList);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);

        iv_goodsGoBack.setOnClickListener(this);
        tv_topRight.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_goodsGoBack:
                finish();
                break;
            case R.id.tv_topRight:
                toast("保存");
                break;
        }
    }

    public void initTypeSelect() {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.management_goods_type_list);
        params.addBodyParameter("seller_id", user.getUser_id());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                String data = JsonUtils.getData(result);
                if (0 == errCode) {
                    tv_no_data.setVisibility(View.GONE);
                    dslvList.setVisibility(View.VISIBLE);
                    goodsTypeList = JSON.parseArray(data, GoodsType.class);
                    typeManagerAdapter = new TypeManagerAdapter(context, goodsTypeList);
                    onDrop = new DragSortListView.DropListener() {
                        @Override
                        public void drop(int from, int to) {
                            if (from != to) {
                                GoodsType goodsType = (GoodsType) typeManagerAdapter.getItem(from);
                                typeManagerAdapter.remove(from);
                                typeManagerAdapter.insert(goodsType, to);
                            }
                        }
                    };
                    onRemove = new DragSortListView.RemoveListener() {
                        @Override
                        public void remove(int which) {
                            typeManagerAdapter.remove(which);
                        }
                    };
                    dslvList.setDropListener(onDrop);
                    dslvList.setRemoveListener(onRemove);
                    dslvList.setAdapter(typeManagerAdapter);
                    dslvList.setDragEnabled(true);
                } else {
                    tv_no_data.setVisibility(View.VISIBLE);
                    dslvList.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }


}
